package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.Firkins;

public class FirkinPresenter extends AbstractPresenter<Firkins> {
	final String SINGLEITEM = "firkins";
	final String MULTIITEM = "firkinss";
	
	public ArrayList<Firkins> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public Firkins tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public Firkins createItemFromJSONObject(JSONObject jsonObject){
		Firkins item = new Firkins();
		try{
			item.setFirkinCost(jsonObject.getDouble("firkCost"));
			item.setFirkinDate(timeFormat.parse(jsonObject.getString("firkDate")));
			item.setFirkinDescription(jsonObject.getString("firkDesc"));
			item.setFirkinID(jsonObject.getInt("firkID"));
			item.setFirkinRecipe(jsonObject.getString("firkRecipe"));
			item.setFirkinResults(jsonObject.getString("firkResults"));
			if(!jsonObject.isNull("firkSalesDate")){
				item.setFirkinSalesDate(timeFormat.parse(jsonObject.getString("firkSalesDate")));
			} else {
				item.setFirkinSalesDate(null);
			}
			item.setFirkinSold(jsonObject.getBoolean("firkSold"));
			item.setFirkinVolume(jsonObject.getDouble("firkVolume"));
			
			if(!jsonObject.isNull("firkCustomerID")){
				//create product if set
				CustomerViewPresenter presenter = new CustomerViewPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("firkCustomerID");
				item.setFirkinCustomerID(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setFirkinCustomerID(null);
			}

			if(!jsonObject.isNull("firkBatchID")){
				//create product if set
				BrewViewPresenter presenter = new BrewViewPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("firkBatchID");
				item.setFirkinBatchID(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setFirkinBatchID(null);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(Firkins item){
		try{
			JSONObject json = new JSONObject();
			json.put("firkCost", item.getFirkinCost());
			json.put("firkDate", getDate(item.getFirkinDate()));
			json.put("firkDesc", item.getFirkinDescription());
			json.put("firkID", item.getFirkinID());
			json.put("firkRecipe", item.getFirkinRecipe());
			json.put("firkResults", item.getFirkinResults());
			if(item.getFirkinSalesDate()!=null){
				json.put("firkSalesDate", getDate(item.getFirkinSalesDate()));
			} else {
				json.put("firkSalesDate", JSONObject.NULL);
			}
			json.put("firkSold", item.isFirkinSold());
			json.put("firkVolume", item.getFirkinVolume());

			if(item.getFirkinCustomerID()!=null){
				final CustomerViewPresenter presenter = new CustomerViewPresenter();
				json.put("firkCustomerID", presenter.createJSON(item.getFirkinCustomerID()));
			} else {
				json.put("firkCustomerID", JSONObject.NULL);
			}

			if(item.getFirkinBatchID()!=null){
				final BrewViewPresenter presenter = new BrewViewPresenter();
				json.put("firkBatchID", presenter.createJSON(item.getFirkinBatchID()));
			} else {
				json.put("firkBatchID", JSONObject.NULL);
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Firkins getEmptyEntity(){
		Firkins entity = new Firkins();
		return entity;
	}
}
